import { createRouter, createWebHashHistory ,RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
//hash模式
const routerHistory = createWebHashHistory()
// createWebHashHistory hash 路由
// createWebHashHistory history 路由
// createMemoryHistory 带缓存 history 路由

import layout from '@/layout/index.vue'

export const contantRoutres :Array<RouteRecordRaw> = [
  {
    path: '/login',
    name:'login',
    component:() => import('@/views/Login/index.vue'),
    meta:{
      hidden:true,
    },
  },
  {
    path: '/excel',
    name:'excel',
    component:() => import('@/views/excel/index.vue'),
    meta:{
      hidden:true,
    },
  },
  {
    path:'/',
    component:layout,
    redirect:'/dashboard',
    children:[{
      path:'dashboard',
      name:'dashboard',
      component:() => import('@/views/dashboard/index.vue'),
      meta:{
        title :'主页',
        icon:'dashboard',
      },
    }],
  },
]
export const asyncRoutes:Array<RouteRecordRaw>= [
  {
    path:'/',
    component:layout,
    children:[{
      path:'departments',
      name:'departments',
      component:() => import('@/views/departments/index.vue'),
      meta:{
        title :'组织架构',
        icon:'tree',
      },
    }],
  },
  {
    path:'/',
    component:layout,
    children:[
      {
        path:'permissons',
        name:'permissions',
        component:() => import('@/views/permissions/permissions.vue'),
        meta:{
          title :'权限管理',
          icon:'good',
        },
      },
    ],
  },
  {
    path:'/',
    component:layout,
    children:[
      {
        path:'settings',
        name:'settings',
        component:() => import('@/views/settings/index.vue'),
        meta:{
          title :'角色管理',
          icon:'settings',
        },
      },
    ],
  },
  {
    path:'/',
    component:layout,
    children:[
      {
        path:'employees',
        name:'employees',
        component:() => import('@/views/employees/index.vue'),
        meta:{
          title :'员工管理',
          icon:'peoples',
        },
      },
      {
        path:'detail',
        name:'detail',
        component:() => import('@/views/employees/detailInfo.vue'),
        meta:{
          title :'员工详情',
          icon:'peoples',
          hidden:true,
        },
      },
    ],
  },

]
const router = createRouter({
  history: routerHistory,
  //routes: [...contantRoutres,...asyncRoutes ],
  routes: [...contantRoutres],
})

//重置路由
export function resetRoute(): void {
  console.log('router.getRoutes()',router.getRoutes())
  router.getRoutes().forEach(route => {
    const { name } = route
    console.log('name',name)
    if (name&&name!=='login'&&name!=='dashboard') {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}



export function setupRouter(app: App<Element>) {
  app.use(router)
}
export default router